import Vue from 'vue'
import VueRouter from 'vue-router'
// ###################################
import Login from '@/components/login'
import Welcome from '@/components/welcome'
import User from '@/user/user'
import Roles from '@/power/roles'
import Rights from '@/power/rights'
import Goods from '@/goods/goods'
import Params from '@/goods/params'
import Categories from '@/goods/categories'
import Orders from '@/order/orders'
import Reports from '@/report/reports'
import Add from '@/goods/goods-add'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: '/login,'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/welcome',
    component: Welcome,
    children: [{
        path: '/users',
        component: User
      },
      {
        path: '/roles',
        component: Roles
      },
      {
        path: '/rights',
        component: Rights
      },
      {
        path: '/goods',
        component: Goods,
      },
      {
        path: '/categories',
        component: Categories
      },
      {
        path: '/params',
        component: Params
      },
      {
        path: '/orders',
        component: Orders
      },
      {
        path: '/reports',
        component: Reports
      }, {
        path: '/goods/add',
        component: Add
      }

    ]
  },
];
const router = new VueRouter({
  routes,
  mode: 'hash', //hash 
});
// 如果用户没有登录，不能访问/home,如果用户通过url地址直接访问，则强制跳转到登录页面
//只要路由值发生改变，就触发这个函数
router.beforeEach(function (to, from, next) {
  //路由导航守卫，前置拦截
  //从本地存储拿到token，并判断
  var token = window.sessionStorage.getItem('token')
  //如果不是从login来的，就没有token，那就不能前进
  if (to.path !== '/login') {
    if (!token) next({
      path: '/login'
    })
    next();
  } else {
    next();
  }
});
// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

export default router;
